package com.giscloud.amp.controller.role;

import com.alibaba.fastjson.JSON;
import com.giscloud.amp.controller.BaseController;
import com.giscloud.amp.entity.dto.AmpRoleDTO;
import com.giscloud.amp.entity.dto.AmpStaffAuthDTO;
import com.giscloud.amp.entity.dto.AmpStaffRoleDTO;
import com.giscloud.amp.entity.vo.AmpRoleVO;
import com.giscloud.amp.entity.vo.AmpStaffAuthVO;
import com.giscloud.amp.entity.vo.AmpStaffRoleVO;
import com.giscloud.amp.entity.vo.SysRegisterVO;
import com.giscloud.amp.service.auth.AmpRoleService;
import com.giscloud.amp.service.auth.AmpStaffAuthService;
import com.giscloud.amp.service.auth.AmpStaffRoleService;
import com.giscloud.amp.service.sys.SysRegisterService;
import com.giscloud.commons.model.Result;
import com.giscloud.commons.utils.StringUtils;
import com.giscloud.datasource.page.PageInfo;
import com.giscloud.token.utils.TokenUserUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.log4j.Log4j2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @Author:gxd
 * @Description:
 * @Date: 10:19 2018/2/14
 * @Modified By:
 */
@SuppressWarnings("SpringJavaAutowiringInspection") //加这个注解让IDE 不报: Could not autowire
@Controller
@Log4j2
public class SystemRoleController extends BaseController {
    @Autowired
    private AmpRoleService ampRoleRemoteClient;
    
    @Autowired
    private AmpStaffRoleService ampStaffRoleRemoteClient;
    
    @Autowired
    private SysRegisterService sysRegisterRemoteClient;

    @Autowired
    private AmpStaffAuthService ampStaffAuthRemoteClient;

    @Autowired
    private TokenUserUtil tokenUserUtil;
    /**
     * 页面
     */
    @RequestMapping(value = "/amp/sysRole/sysRoleList", method = RequestMethod.GET)
    public Result sysRolePage()throws Exception{

        List<SysRegisterVO> sysRegisterVOS = sysRegisterRemoteClient.listSysRegister();
        List<Map> list = Lists.newArrayListWithCapacity(8);
        for (SysRegisterVO sysRegisterVO : sysRegisterVOS) {
            Map map = Maps.newHashMapWithExpectedSize(2);
            map.put("systemId",sysRegisterVO.getSystemId());
            map.put("systemName",sysRegisterVO.getSystemName());
            list.add(map);
        }

        return Result.success(list);
    }
    @RequestMapping(value="/amp/sysRole/getAmpRoleList",method = RequestMethod.POST)
    @ResponseBody
    public PageInfo getAmpRoleList(@RequestBody AmpRoleDTO ampRoleDTO) {
        return ampRoleRemoteClient.getAmpRoleList(ampRoleDTO);
    }

    @RequestMapping(value="/amp/sysRole/add",method = RequestMethod.POST)
    @ResponseBody
    public Result add(@RequestBody AmpRoleDTO ampRoleDTO){
        Result result = new Result();

            AmpRoleVO ampRoleVO = ampRoleRemoteClient.getAmpRoleByCondition(ampRoleDTO);
            if (ampRoleVO != null) {
                result.setMsg("该系统中已经存在该角色！");
                result.setCode(-1);
                result.setSuccess(false);
                return result;
            }
            int res = ampRoleRemoteClient.insert(ampRoleDTO);
            if(res >= 1){
                result.setMsg("提交成功");
                result.setCode(0);
                result.setSuccess(true);
            }else{
                result.setMsg("提交失败");
                result.setCode(-1);
                result.setSuccess(false);
            }

        return result;
    }
    @RequestMapping(value="/amp/sysRole/edit",method = RequestMethod.POST)
    @ResponseBody
    public Result edit(@RequestBody AmpRoleDTO ampRoleDTO){
        Result result = new Result();

            ampRoleDTO.setCreatedStaffId(tokenUserUtil.getUserId());
            AmpRoleDTO ampRoleDTOByCondition = new AmpRoleDTO();
            BeanUtils.copyProperties(ampRoleDTO,ampRoleDTOByCondition);
            ampRoleDTOByCondition.setRoleName("");
            AmpRoleVO ampRoleVO = ampRoleRemoteClient.getAmpRoleByCondition(ampRoleDTOByCondition);
            ampRoleDTO.setOldValue(StringUtils.getOldValue(ampRoleVO));
            ampRoleDTO.setCreatedStaffId(tokenUserUtil.getUserId());
            if(ampRoleRemoteClient.update(ampRoleDTO) >= 1){
                result.setMsg("更新成功");
                result.setCode(0);
                result.setSuccess(true);
            }else{
                result.setMsg("更新失败");
                result.setCode(-1);
                result.setSuccess(false);
            }

        return result;
    }

    @RequestMapping(value="/amp/sysRole/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestBody AmpRoleDTO ampRoleDTO){
        Result result = new Result();
        if(ampRoleDTO.getRoleId() == null){
            result.setMsg("删除失败，系统ID不能为空");
            result.setCode(-1);
            result.setSuccess(false);
            return result;
        }
       // try {
        	//查询该角色权限是否分配给其他人
        	AmpStaffRoleDTO ampStaffRoleDTO = new AmpStaffRoleDTO();
        	ampStaffRoleDTO.setRoleId(ampRoleDTO.getRoleId());
        	ampStaffRoleDTO.setStatusCd((short)1);
        	AmpStaffRoleVO ampStaffRoleVO =ampStaffRoleRemoteClient.getAmpStaffRoleByCondition(ampStaffRoleDTO);
            if(ampStaffRoleVO != null){
            	 result.setMsg("有用户拥有该权限，删除失败");
                 result.setCode(-1);
                 result.setSuccess(false);
                 return result;
            }
            log.info("删除的roleid为："+ampRoleDTO.getRoleId());
        	if(ampRoleRemoteClient.delete(ampRoleDTO.getRoleId()) >= 1){
                result.setMsg("删除成功");
                result.setCode(0);
                result.setSuccess(true);
            }else{
                result.setMsg("删除失败");
                result.setCode(-1);
                result.setSuccess(false);
            }
       /* }catch (Exception e){
            result.setMsg("删除失败");
            result.setCode(-1);
            result.setSuccess(false);
        }*/
        return result;
    }

    @RequestMapping(value="/amp/sysRole/findAmpRoleAuthList")
    @ResponseBody
    public List<AmpStaffAuthVO> findAmpRoleAuthList(@RequestBody AmpStaffAuthDTO ampStaffAuthDTO) {
    	if(ampStaffAuthDTO.getObjectId()==null || ampStaffAuthDTO.getSystemId()==null)
        {
            List<AmpStaffAuthVO> alist=new ArrayList<AmpStaffAuthVO>();
            return alist;
        }
        ampStaffAuthDTO.setStaffId(tokenUserUtil.getUserId());
        ampStaffAuthDTO.setObjectType((short)2);

        //根据所属系统获取所有的权限列表
        List<AmpStaffAuthVO> list=ampStaffAuthRemoteClient.findAmpRoleAuthList(ampStaffAuthDTO);
        
        return list;
    }
    @RequestMapping(value="/amp/sysRole/roleAuthAdd",method = RequestMethod.POST)
    @ResponseBody
    public Result roleAuthAdd(@RequestBody AmpStaffAuthDTO ampStaffAuthDTO){
        Result result = new Result();

            ampStaffAuthDTO.setCreatedStaffId(tokenUserUtil.getUserId());
            if(ampStaffAuthRemoteClient.insert(ampStaffAuthDTO) >= 1){
                result.setMsg("操作成功");
                result.setCode(0);
                result.setSuccess(true);
            }else{
                result.setMsg("操作失败");
                result.setCode(-1);
                result.setSuccess(false);
            }

        return result;
    }

    @RequestMapping(value="/amp/sysRole/roleAuthEdit",method = RequestMethod.POST)
    @ResponseBody
    public Result roleAuthEdit(@RequestBody AmpStaffAuthDTO ampStaffAuthDTO){
        Result result = new Result();
        ampStaffAuthDTO.setCreatedStaffId(tokenUserUtil.getUserId());
        if(ampStaffAuthRemoteClient.update(ampStaffAuthDTO) >= 1){
            result.setMsg("操作成功");
            result.setCode(0);
            result.setSuccess(true);
        }else{
            result.setMsg("操作失败");
            result.setCode(-1);
            result.setSuccess(false);
        }
        return result;
    }
    @RequestMapping(value="/amp/sysRole/roleAuthDel",method = RequestMethod.POST)
    @ResponseBody
    public Result roleAuthDel(@RequestBody AmpStaffAuthDTO ampStaffAuthDTO){
        Result result = new Result();
        if(ampStaffAuthDTO.getRelId() == null){
            result.setMsg("删除失败，关系ID不能为空");
            result.setCode(-1);
            result.setSuccess(false);
            return result;
        }
        if(ampStaffAuthRemoteClient.delete(ampStaffAuthDTO.getRelId()) >= 1){
            result.setMsg("删除成功");
            result.setCode(0);
            result.setSuccess(true);
        }else{
            result.setMsg("删除失败");
            result.setCode(-1);
            result.setSuccess(false);
        }
        return result;
    }
    @RequestMapping(value="/amp/sysRole/batRoleAuthAdd",method = RequestMethod.POST)
    @ResponseBody
    public Result batRoleAuthAdd(@RequestBody List<AmpStaffAuthDTO> ampStaffAuthDTOList){
        Result result = new Result();

            if(!ampStaffAuthDTOList.isEmpty() && ampStaffAuthDTOList.size()!=0)
            {
                for(int i=0;i<ampStaffAuthDTOList.size();i++){
                    AmpStaffAuthDTO ampStaffAuthDTO = ampStaffAuthDTOList.get(i);
                    ampStaffAuthDTO.setCreatedStaffId(tokenUserUtil.getUserId());
                    ampStaffAuthDTO.setOpreateStaffId(tokenUserUtil.getUserId());
                    if(ampStaffAuthDTO.getRelId()==-1){
                        if(ampStaffAuthRemoteClient.insert(ampStaffAuthDTO) >= 1){
                            result.setMsg("操作成功");
                            result.setCode(0);
                            result.setSuccess(true);
                        }else{
                            result.setMsg("操作失败");
                            result.setCode(-1);
                            result.setSuccess(false);
                        }
                    }
                    else
                    {
                        if(ampStaffAuthRemoteClient.update(ampStaffAuthDTO) >= 1){
                            result.setMsg("操作成功");
                            result.setCode(0);
                            result.setSuccess(true);
                        }else{
                            result.setMsg("操作失败");
                            result.setCode(-1);
                            result.setSuccess(false);
                        }
                    }
                }
            }

        return result;
    }

}
